package com.adk.backend.controller;

import com.adk.backend.common.Result;
import com.adk.backend.entity.LxOrderList;
import com.adk.backend.entity.LxRecvLogs;
import com.adk.backend.service.OrderService;
import com.adk.backend.service.RecvLogsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 仓库收货控制器
 */
@Slf4j
@RestController
@RequestMapping("/receive")
public class ReceiveController {
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private RecvLogsService recvLogsService;
    
    /**
     * 根据箱单条码获取订单信息
     * 对应原PHP接口：/get_ctn
     */
    @PostMapping("/get_ctn")
    public Result<Map<String, Object>> getCtnInfo(@RequestParam String barcode) {
        try {
            // 解析条码，格式通常是：订单号-箱号，如：ADK20240101001-1
            String[] parts = barcode.split("-");
            if (parts.length < 2) {
                return Result.error("条码格式错误");
            }
            
            String orderNo = parts[0];
            Integer ctnNo = Integer.parseInt(parts[1]);
            
            // 查询订单信息
            LxOrderList order = orderService.getOrderByNo(orderNo);
            if (order == null) {
                return Result.error("订单不存在");
            }
            
            // 查询已收货箱数
            Integer recvCtns = recvLogsService.countRecvCtnsByOrderId(orderNo);
            Integer unrecCtns = order.getTotalCtns() - recvCtns;
            
            // 查询该箱的收货记录
            LxRecvLogs recvLog = recvLogsService.getByOrderIdAndCtnNo(orderNo, ctnNo);
            
            Map<String, Object> result = new HashMap<>();
            result.put("order_id", order.getOrderNo());
            result.put("ol_id", order.getId());
            result.put("total_ctns", order.getTotalCtns());
            result.put("unrec_ctns", unrecCtns);
            result.put("ctn_no", ctnNo);
            result.put("ship_type", order.getShipType());
            result.put("wm_remarks", order.getWmsRemarks());
            
            // 如果有收货记录，返回已上传的照片
            if (recvLog != null && recvLog.getImg() != null) {
                String[] imgs = recvLog.getImg().split(",");
                result.put("img", imgs);
            } else {
                result.put("img", new String[0]);
            }
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取箱单信息失败", e);
            return Result.error("获取箱单信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 保存收货记录
     * 对应原PHP接口：/save_recv
     */
    @PostMapping("/save_recv")
    public Result<Boolean> saveRecv(@RequestBody LxRecvLogs recvLogs) {
        try {
            boolean result = recvLogsService.saveRecv(recvLogs);
            if (result) {
                return Result.success("收货成功", true);
            }
            return Result.error("收货失败");
        } catch (Exception e) {
            log.error("保存收货记录失败", e);
            return Result.error("保存收货记录失败：" + e.getMessage());
        }
    }
}

